package com.shop.web.controller;

import com.shop.util.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Controller
@Slf4j
public class BaseController {

    public static int DOWNLOAD_EXCEL_MAX_ROWS = 999999;

    @Autowired
    private HttpServletResponse response;

    public <T> void downloadExcel(String fileName, List<T> list,
                              String[] titles, String[] fieldNames) {
        try {
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xls");
            ExcelUtil.export(response.getOutputStream(), fileName, list, titles, fieldNames);
        } catch (Exception e) {
            log.error("导出" + fileName + "异常！", e);
        }
    }

}
